package views;

import bean.Cls;
import bean.Student;
import bean.Team;
import service.ClassService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class AddStudent extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;// 主面板
    private JTextField textField;
    private JTextField textField_1;
    Cls cls2=new Cls();
    Team team=new Team();
    Student student=new Student();
    JComboBox comboBox = new JComboBox();// 小组下拉框
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddStudent frame = new AddStudent();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    // 设置Cls对象并查询小组列表
    public  void setCls(Cls cls) {
        cls2=cls;

        //查询小组列表
        ClassService s=new ClassService();
        List<Team> aList=s.selectTeam();
        Vector data =new Vector();
        Iterator<Team> iterator=aList.iterator();
        while(iterator.hasNext()){
            Team next = iterator.next();
            comboBox.addItem(next.getT_name());


        }


    }

    /**
     * Create the frame.
     */
    public AddStudent() {
        setTitle("新增学生");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 348, 261);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("新增学生");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        lblNewLabel.setBounds(103, 10, 174, 58);
        contentPane.add(lblNewLabel);

        // 添加标签和输入框
        JLabel lblNewLabel_1 = new JLabel("小组：");
        lblNewLabel_1.setBounds(42, 84, 73, 15);
        contentPane.add(lblNewLabel_1);

        // 新增按钮
        JButton btnNewButton = new JButton("新增");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(textField.getText().length()==0||textField_1.getText().length()==0) {
                    JOptionPane.showMessageDialog(null,"请输入学号或姓名！！","提示",JOptionPane.PLAIN_MESSAGE);
                }else {


                    //通过小组名称获取小组ID
                    ClassService classService=new ClassService();
                    Team team2=new Team();
                    team2.setC_id(cls2.getC_id());
                    team2.setT_name(comboBox.getSelectedItem().toString());
                    team=classService.selectTeamByName2(team2);

                    //封装数据到student实体类里面
                    student.setC_id(cls2.getC_id());
                    student.setT_id(team.getT_id());
                    student.setS_id(textField.getText());
                    student.setS_name(textField_1.getText());

                    int n=classService.insertStudent(student);
                    if(n>0) {
                        JOptionPane.showMessageDialog(null,"添加成功！","提示",JOptionPane.PLAIN_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(null,"添加失败！","提示",JOptionPane.PLAIN_MESSAGE);
                    }
                }

            }
        });
        btnNewButton.setBounds(80, 181, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("返回");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();
                m.setCls(cls2);
                m.setVisible(true);
                AddStudent.this.dispose();
            }
        });
        btnNewButton_1.setBounds(183, 181, 93, 23);
        contentPane.add(btnNewButton_1);

        // 小组下拉框
        comboBox.setBounds(80, 80, 196, 23);
        contentPane.add(comboBox);

        // 学号和姓名标签及输入框
        JLabel lblNewLabel_1_1 = new JLabel("学号：");
        lblNewLabel_1_1.setBounds(42, 113, 73, 15);
        contentPane.add(lblNewLabel_1_1);

        JLabel lblNewLabel_1_2 = new JLabel("姓名：");
        lblNewLabel_1_2.setBounds(42, 142, 73, 15);
        contentPane.add(lblNewLabel_1_2);

        textField = new JTextField();
        textField.setBounds(80, 109, 196, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(80, 138, 196, 21);
        contentPane.add(textField_1);
    }
}
